/**
 * @param {number[][]} matrix
 * @param {number} target
 * @return {boolean}
 */
var searchMatrix = function(matrix, target) {
  const [m, n] = [matrix.length, matrix[0].length];

  for (let i = 0; i < m; i++) {
    let res = search(matrix, 1, i, n, target);
    if (res) {
      return true;
    }
  }
  return false;
};

function search(matrix, flag, index, len, target) {
// 行
if (flag) {
  let left = 0, right = len - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (matrix[index][mid] === target) {
      return true;
    } 
    if (matrix[index][mid] > target) {
      right = mid - 1;
    }
    if (matrix[index][mid] < target) {
      left = mid + 1;
    }
  }
  return false;
}
// 列 --- 没啥必要，我是按行的
else {
  let left = 0, right = len - 1;
  while (left < right) {
    let mid = Math.floor((left + right) / 2);
    if (matrix[mid][index] === target) {
      return true;
    } 
    if (matrix[mid][index] > target) {
      right = mid - 1;
    }
    if (matrix[mid][index] < target) {
      left = mid + 1;
    }
  }
  return false;
}
}


const matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5;

console.log(searchMatrix(matrix, target));